require "makeUpStrength.lua"
require "makeUpBlendMode.lua"
require "beautyStrength.lua"
require "lipLayer.lua"
require "config.lua"
require "UIScrollBar_190307.lua"

makeup = nil
eyeLinerPath = nil
oldEyeShadowLayer1BlendMode = nil
oldEyeShadowLayer2BlendMode = nil


oldLipLayerBlendModes = {nil, nil, nil}
lipLayers = {nil, nil, nil}

function initialize(scene)

  makeup = KuruMakeupNode.create(true)
  scene:addNodeAndRelease(makeup)

  makeup:setPathAndBlendMode(KuruMakeupNodeType.BLUSH, BASE_DIRECTORY .. "img/blush.png", blushBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.CONTOUR, BASE_DIRECTORY .. "img/contour.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.LIP_COLOR, BASE_DIRECTORY .. "img/lip_color.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYE_COLOR, BASE_DIRECTORY .. "img/eye_color_0001/eye_color_0001.gpb", eyeColorBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYEBROWS, BASE_DIRECTORY .. "img/eyebrows.png", eyeBrowsBlendMode)

  --eyeShadow
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER1, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L1.png", eyeShadowLayer1BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER2, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L2.png", eyeShadowLayer2BlendMode)

  --eyeLiner

  eyeLinerPath = BASE_DIRECTORY .. "img/eye_liner/eye_liner.png"
  if (DeviceConfig.instance().platformType == DeviceConfigPlatformType.P_IOS) then
    local eyeLinerPath_iOS = BASE_DIRECTORY .. "img/eye_liner_iOS/eye_liner_iOS.png"
    local file = io.open(eyeLinerPath_iOS, "r")
    if file ~= nil then
      eyeLinerPath = eyeLinerPath_iOS
    end
  end

  print("eyeLinerPath = " .. eyeLinerPath)

  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELINER, eyeLinerPath, eyeLinerBlendMode)

  --eyeLashes
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELASHES, BASE_DIRECTORY .. "img/eyelashes/eyelashes.png", eyeLashesBlendMode)

  EngineStatus.instance():setBoolean("useBuiltInDistortionInScript", true)

  distortionNode = KaleFaceDistortionNode.create()
  distortionNode:loadDistortionFromString(SceneConfig.instance().distortionJson)
  scene:addNode(distortionNode)
  distortionNode:release()


  makeup_param = makeup:getParam()

  -- beauty
  makeup_param.laughLine = laughLine
  makeup_param.eyeLut = eyeLut
  makeup_param.eyePlim = eyePlim
  makeup_param.darkCircle = darkCircle
  makeup_param.whiteTeeth = whiteTeeth

  -- makeup
  makeup_param.faceContour = faceContour
  makeup_param.cheek = cheek
  makeup_param.lip = lip
  makeup_param.colorLens = colorLens
  makeup_param.eyeBrow = eyeBrow
  makeup_param.eyeShadow = eyeShadow
  makeup_param.eyeLiner = eyeLiner
  makeup_param.eyeLashes = eyeLashes

  g_scrollBar = UIScrollBar:init(scene, 1.0)

end





function frameReady(scene, elapsedTime)
  g_scrollBar:frameReady()

  -- ================서버에 등록 할 때 삭제===================
  setParameters()

  makeup:setPathAndBlendMode(KuruMakeupNodeType.BLUSH, BASE_DIRECTORY .. "img/blush.png", blushBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.CONTOUR, BASE_DIRECTORY .. "img/contour.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.LIP_COLOR, BASE_DIRECTORY .. "img/lip_color.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYE_COLOR, BASE_DIRECTORY .. "img/eye_color_0001/eye_color_0001.gpb", eyeColorBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYEBROWS, BASE_DIRECTORY .. "img/eyebrows.png", eyeBrowsBlendMode)

  --eyeShadow

  if oldEyeShadowLayer1BlendMode ~= eyeShadowLayer1BlendMode then
    oldEyeShadowLayer1BlendMode = eyeShadowLayer1BlendMode
    makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode + 1)
  end

  if oldEyeShadowLayer2BlendMode ~= eyeShadowLayer2BlendMode then
    oldEyeShadowLayer2BlendMode = eyeShadowLayer2BlendMode
    makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode + 1)
  end
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER1, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L1.png", eyeShadowLayer1BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER2, BASE_DIRECTORY .. "img/eye_shadow/eye_shadow_L2.png", eyeShadowLayer2BlendMode)

  --eyeLiner
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELINER, BASE_DIRECTORY .. "img/eye_liner/eye_liner.png", eyeLinerBlendMode)

  --eyeLashes
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELASHES, BASE_DIRECTORY .. "img/eyelashes/eyelashes.png", eyeLashesBlendMode)

  reBuildLipLayers(scene)
  for i=1, 3 do
    lipLayers[i]:setEnabled(lipLayer > 0.0)
    lipLayers[i]:setModulateAlpha(lipLayer)
  end

  local strength = g_scrollBar:getStrength()
  -- beauty
  makeup_param.laughLine = laughLine * strength
  makeup_param.eyeLut = eyeLut * strength
  makeup_param.eyePlim = eyePlim * strength
  makeup_param.darkCircle = darkCircle * strength
  makeup_param.whiteTeeth = whiteTeeth * strength

  -- makeup
  makeup_param.faceContour = faceContour * strength
  makeup_param.cheek = cheek * strength
  makeup_param.lip = lip * strength
  makeup_param.colorLens = colorLens * strength
  makeup_param.eyeBrow = eyeBrow * strength
  makeup_param.eyeShadow = eyeShadow * strength
  makeup_param.eyeLiner = eyeLiner * strength
  makeup_param.eyeLashes = eyeLashes * strength

end


function reBuildLipLayers(scene)

  lipLayerPaths = {"img/lip_layer/lip_layer0.png", "img/lip_layer/lip_layer1.png", "img/lip_layer/lip_layer2.png"}
  curBlendModes = {lipLayer0BlendMode, lipLayer1BlendMode, lipLayer2BlendMode}

  for i=1, 3 do
    if nil == lipLayers[i] or oldLipLayerBlendModes[i] ~= curBlendModes[i] then
      if lipLayers[i] ~= nil then
        scene:removeNode(lipLayers[i])
      end

      lipLayers[i] = KaleFaceSkinNode.create(
      KaleFaceSkinNodeBuilder.create()
      :path(BASE_DIRECTORY .. lipLayerPaths[i])
      :skinType(KaleFaceSkinType.FACE_EX)
      :blendmode(curBlendModes[i])
      :build())
      if(nil ~= lipLayers[i]) then
        scene:addNodeAndRelease(lipLayers[i])
      end
    end

    oldLipLayerBlendModes[i] = curBlendModes[i]

    scene:removeNode(distortionNode)

    distortionNode = KaleFaceDistortionNode.create()
    distortionNode:loadDistortionFromString(SceneConfig.instance().distortionJson)
    scene:addNode(distortionNode)
    distortionNode:release()
  end


  --  d1 = KaleFaceDistortionNode.createWithPath(BASE_DIRECTORY .. "d.json")
  --  scene:addNodeAndRelease(d1)

end


function finalize(scene)
  g_scrollBar:finalize()
end
